$(function () {
    //自定义校验规则的validator
    $.validator.addMethod(
        "checkUsername",
        function (value,element,params) {
                //定义一个变量存储该函数的返回值
                var flag=true;
                //偷偷的到后端请求
                $.ajax({
                    async:false,//同步操作
                    type:"post",
                    url:"user?method=checkUsername",
                    data:"username="+value,
                    dataType:"json",
                    success:function (data) {
                        flag=data.isExist;
                    }
                });
                return !flag;
        }
    )

    $("#registerForm").validate({
        rules:{
            "username":{
                required:true,
                checkUsername:true
            },
            "password":{
                required:true,
                rangelength:[6,12]
            },
            "repassword":{
                required:true,
                rangelength:[6,12],
                equalTo:"#password"
            },
            "email":{
                required:true,
                email: true
            }
        },
        messages:{
            "username":{
                required: "用户名不能为空!",
                checkUsername:"该用户名已存在"
            },
            "password":{
                required:"密码不能为空",
                rangelength: "密码的长度必须在6-12之间"
            },
            "repassword":{
                required:"确认密码不能为空",
                rangelength:"确认密码的长度必须在6-12之间",
                equalTo: "两次密码不一致"
            },
            "email":{
                required:"邮箱不能为空",
                email:"邮箱的格式不正确"
            }
        }
    })
})